/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Monitoring;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Technicalcollection;
import entityBeans.Surroundingsculture;
import entityBeans.Supporttype;
import entityBeans.Predominantbiome;
import entityBeans.Digitizer;
import entityBeans.Denomination;
import entityBeans.Culture;
import entityBeans.Cultivar;
import entityBeans.Colorpantrap;
import entityBeans.Collector;
import entityBeans.Specimen;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class MonitoringJpaController {

    public MonitoringJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Monitoring monitoring) {
        if (monitoring.getSpecimenList() == null) {
            monitoring.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Technicalcollection technicalcollection = monitoring.getTechnicalcollection();
            if (technicalcollection != null) {
                technicalcollection = em.getReference(technicalcollection.getClass(), technicalcollection.getIdtechnicalcollection());
                monitoring.setTechnicalcollection(technicalcollection);
            }
            Surroundingsculture surroundingsculture = monitoring.getSurroundingsculture();
            if (surroundingsculture != null) {
                surroundingsculture = em.getReference(surroundingsculture.getClass(), surroundingsculture.getIdsurroundingsculture());
                monitoring.setSurroundingsculture(surroundingsculture);
            }
            Supporttype supporttype = monitoring.getSupporttype();
            if (supporttype != null) {
                supporttype = em.getReference(supporttype.getClass(), supporttype.getIdsupporttype());
                monitoring.setSupporttype(supporttype);
            }
            Predominantbiome predominantbiome = monitoring.getPredominantbiome();
            if (predominantbiome != null) {
                predominantbiome = em.getReference(predominantbiome.getClass(), predominantbiome.getIdpredominantbiome());
                monitoring.setPredominantbiome(predominantbiome);
            }
            Digitizer digitizer = monitoring.getDigitizer();
            if (digitizer != null) {
                digitizer = em.getReference(digitizer.getClass(), digitizer.getIddigitizer());
                monitoring.setDigitizer(digitizer);
            }
            Denomination denomination = monitoring.getDenomination();
            if (denomination != null) {
                denomination = em.getReference(denomination.getClass(), denomination.getIddenomination());
                monitoring.setDenomination(denomination);
            }
            Culture culture = monitoring.getCulture();
            if (culture != null) {
                culture = em.getReference(culture.getClass(), culture.getIdculture());
                monitoring.setCulture(culture);
            }
            Cultivar cultivar = monitoring.getCultivar();
            if (cultivar != null) {
                cultivar = em.getReference(cultivar.getClass(), cultivar.getIdcultivar());
                monitoring.setCultivar(cultivar);
            }
            Colorpantrap colorpantrap = monitoring.getColorpantrap();
            if (colorpantrap != null) {
                colorpantrap = em.getReference(colorpantrap.getClass(), colorpantrap.getIdcolorpantrap());
                monitoring.setColorpantrap(colorpantrap);
            }
            Collector collector = monitoring.getCollector();
            if (collector != null) {
                collector = em.getReference(collector.getClass(), collector.getIdcollector());
                monitoring.setCollector(collector);
            }
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : monitoring.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            monitoring.setSpecimenList(attachedSpecimenList);
            em.persist(monitoring);
            if (technicalcollection != null) {
                technicalcollection.getMonitoringList().add(monitoring);
                technicalcollection = em.merge(technicalcollection);
            }
            if (surroundingsculture != null) {
                surroundingsculture.getMonitoringList().add(monitoring);
                surroundingsculture = em.merge(surroundingsculture);
            }
            if (supporttype != null) {
                supporttype.getMonitoringList().add(monitoring);
                supporttype = em.merge(supporttype);
            }
            if (predominantbiome != null) {
                predominantbiome.getMonitoringList().add(monitoring);
                predominantbiome = em.merge(predominantbiome);
            }
            if (digitizer != null) {
                digitizer.getMonitoringList().add(monitoring);
                digitizer = em.merge(digitizer);
            }
            if (denomination != null) {
                denomination.getMonitoringList().add(monitoring);
                denomination = em.merge(denomination);
            }
            if (culture != null) {
                culture.getMonitoringList().add(monitoring);
                culture = em.merge(culture);
            }
            if (cultivar != null) {
                cultivar.getMonitoringList().add(monitoring);
                cultivar = em.merge(cultivar);
            }
            if (colorpantrap != null) {
                colorpantrap.getMonitoringList().add(monitoring);
                colorpantrap = em.merge(colorpantrap);
            }
            if (collector != null) {
                collector.getMonitoringList().add(monitoring);
                collector = em.merge(collector);
            }
            for (Specimen specimenListSpecimen : monitoring.getSpecimenList()) {
                Monitoring oldMonitoringOfSpecimenListSpecimen = specimenListSpecimen.getMonitoring();
                specimenListSpecimen.setMonitoring(monitoring);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldMonitoringOfSpecimenListSpecimen != null) {
                    oldMonitoringOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldMonitoringOfSpecimenListSpecimen = em.merge(oldMonitoringOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Monitoring monitoring) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Monitoring persistentMonitoring = em.find(Monitoring.class, monitoring.getIdmonitoring());
            Technicalcollection technicalcollectionOld = persistentMonitoring.getTechnicalcollection();
            Technicalcollection technicalcollectionNew = monitoring.getTechnicalcollection();
            Surroundingsculture surroundingscultureOld = persistentMonitoring.getSurroundingsculture();
            Surroundingsculture surroundingscultureNew = monitoring.getSurroundingsculture();
            Supporttype supporttypeOld = persistentMonitoring.getSupporttype();
            Supporttype supporttypeNew = monitoring.getSupporttype();
            Predominantbiome predominantbiomeOld = persistentMonitoring.getPredominantbiome();
            Predominantbiome predominantbiomeNew = monitoring.getPredominantbiome();
            Digitizer digitizerOld = persistentMonitoring.getDigitizer();
            Digitizer digitizerNew = monitoring.getDigitizer();
            Denomination denominationOld = persistentMonitoring.getDenomination();
            Denomination denominationNew = monitoring.getDenomination();
            Culture cultureOld = persistentMonitoring.getCulture();
            Culture cultureNew = monitoring.getCulture();
            Cultivar cultivarOld = persistentMonitoring.getCultivar();
            Cultivar cultivarNew = monitoring.getCultivar();
            Colorpantrap colorpantrapOld = persistentMonitoring.getColorpantrap();
            Colorpantrap colorpantrapNew = monitoring.getColorpantrap();
            Collector collectorOld = persistentMonitoring.getCollector();
            Collector collectorNew = monitoring.getCollector();
            List<Specimen> specimenListOld = persistentMonitoring.getSpecimenList();
            List<Specimen> specimenListNew = monitoring.getSpecimenList();
            if (technicalcollectionNew != null) {
                technicalcollectionNew = em.getReference(technicalcollectionNew.getClass(), technicalcollectionNew.getIdtechnicalcollection());
                monitoring.setTechnicalcollection(technicalcollectionNew);
            }
            if (surroundingscultureNew != null) {
                surroundingscultureNew = em.getReference(surroundingscultureNew.getClass(), surroundingscultureNew.getIdsurroundingsculture());
                monitoring.setSurroundingsculture(surroundingscultureNew);
            }
            if (supporttypeNew != null) {
                supporttypeNew = em.getReference(supporttypeNew.getClass(), supporttypeNew.getIdsupporttype());
                monitoring.setSupporttype(supporttypeNew);
            }
            if (predominantbiomeNew != null) {
                predominantbiomeNew = em.getReference(predominantbiomeNew.getClass(), predominantbiomeNew.getIdpredominantbiome());
                monitoring.setPredominantbiome(predominantbiomeNew);
            }
            if (digitizerNew != null) {
                digitizerNew = em.getReference(digitizerNew.getClass(), digitizerNew.getIddigitizer());
                monitoring.setDigitizer(digitizerNew);
            }
            if (denominationNew != null) {
                denominationNew = em.getReference(denominationNew.getClass(), denominationNew.getIddenomination());
                monitoring.setDenomination(denominationNew);
            }
            if (cultureNew != null) {
                cultureNew = em.getReference(cultureNew.getClass(), cultureNew.getIdculture());
                monitoring.setCulture(cultureNew);
            }
            if (cultivarNew != null) {
                cultivarNew = em.getReference(cultivarNew.getClass(), cultivarNew.getIdcultivar());
                monitoring.setCultivar(cultivarNew);
            }
            if (colorpantrapNew != null) {
                colorpantrapNew = em.getReference(colorpantrapNew.getClass(), colorpantrapNew.getIdcolorpantrap());
                monitoring.setColorpantrap(colorpantrapNew);
            }
            if (collectorNew != null) {
                collectorNew = em.getReference(collectorNew.getClass(), collectorNew.getIdcollector());
                monitoring.setCollector(collectorNew);
            }
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            monitoring.setSpecimenList(specimenListNew);
            monitoring = em.merge(monitoring);
            if (technicalcollectionOld != null && !technicalcollectionOld.equals(technicalcollectionNew)) {
                technicalcollectionOld.getMonitoringList().remove(monitoring);
                technicalcollectionOld = em.merge(technicalcollectionOld);
            }
            if (technicalcollectionNew != null && !technicalcollectionNew.equals(technicalcollectionOld)) {
                technicalcollectionNew.getMonitoringList().add(monitoring);
                technicalcollectionNew = em.merge(technicalcollectionNew);
            }
            if (surroundingscultureOld != null && !surroundingscultureOld.equals(surroundingscultureNew)) {
                surroundingscultureOld.getMonitoringList().remove(monitoring);
                surroundingscultureOld = em.merge(surroundingscultureOld);
            }
            if (surroundingscultureNew != null && !surroundingscultureNew.equals(surroundingscultureOld)) {
                surroundingscultureNew.getMonitoringList().add(monitoring);
                surroundingscultureNew = em.merge(surroundingscultureNew);
            }
            if (supporttypeOld != null && !supporttypeOld.equals(supporttypeNew)) {
                supporttypeOld.getMonitoringList().remove(monitoring);
                supporttypeOld = em.merge(supporttypeOld);
            }
            if (supporttypeNew != null && !supporttypeNew.equals(supporttypeOld)) {
                supporttypeNew.getMonitoringList().add(monitoring);
                supporttypeNew = em.merge(supporttypeNew);
            }
            if (predominantbiomeOld != null && !predominantbiomeOld.equals(predominantbiomeNew)) {
                predominantbiomeOld.getMonitoringList().remove(monitoring);
                predominantbiomeOld = em.merge(predominantbiomeOld);
            }
            if (predominantbiomeNew != null && !predominantbiomeNew.equals(predominantbiomeOld)) {
                predominantbiomeNew.getMonitoringList().add(monitoring);
                predominantbiomeNew = em.merge(predominantbiomeNew);
            }
            if (digitizerOld != null && !digitizerOld.equals(digitizerNew)) {
                digitizerOld.getMonitoringList().remove(monitoring);
                digitizerOld = em.merge(digitizerOld);
            }
            if (digitizerNew != null && !digitizerNew.equals(digitizerOld)) {
                digitizerNew.getMonitoringList().add(monitoring);
                digitizerNew = em.merge(digitizerNew);
            }
            if (denominationOld != null && !denominationOld.equals(denominationNew)) {
                denominationOld.getMonitoringList().remove(monitoring);
                denominationOld = em.merge(denominationOld);
            }
            if (denominationNew != null && !denominationNew.equals(denominationOld)) {
                denominationNew.getMonitoringList().add(monitoring);
                denominationNew = em.merge(denominationNew);
            }
            if (cultureOld != null && !cultureOld.equals(cultureNew)) {
                cultureOld.getMonitoringList().remove(monitoring);
                cultureOld = em.merge(cultureOld);
            }
            if (cultureNew != null && !cultureNew.equals(cultureOld)) {
                cultureNew.getMonitoringList().add(monitoring);
                cultureNew = em.merge(cultureNew);
            }
            if (cultivarOld != null && !cultivarOld.equals(cultivarNew)) {
                cultivarOld.getMonitoringList().remove(monitoring);
                cultivarOld = em.merge(cultivarOld);
            }
            if (cultivarNew != null && !cultivarNew.equals(cultivarOld)) {
                cultivarNew.getMonitoringList().add(monitoring);
                cultivarNew = em.merge(cultivarNew);
            }
            if (colorpantrapOld != null && !colorpantrapOld.equals(colorpantrapNew)) {
                colorpantrapOld.getMonitoringList().remove(monitoring);
                colorpantrapOld = em.merge(colorpantrapOld);
            }
            if (colorpantrapNew != null && !colorpantrapNew.equals(colorpantrapOld)) {
                colorpantrapNew.getMonitoringList().add(monitoring);
                colorpantrapNew = em.merge(colorpantrapNew);
            }
            if (collectorOld != null && !collectorOld.equals(collectorNew)) {
                collectorOld.getMonitoringList().remove(monitoring);
                collectorOld = em.merge(collectorOld);
            }
            if (collectorNew != null && !collectorNew.equals(collectorOld)) {
                collectorNew.getMonitoringList().add(monitoring);
                collectorNew = em.merge(collectorNew);
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setMonitoring(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Monitoring oldMonitoringOfSpecimenListNewSpecimen = specimenListNewSpecimen.getMonitoring();
                    specimenListNewSpecimen.setMonitoring(monitoring);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldMonitoringOfSpecimenListNewSpecimen != null && !oldMonitoringOfSpecimenListNewSpecimen.equals(monitoring)) {
                        oldMonitoringOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldMonitoringOfSpecimenListNewSpecimen = em.merge(oldMonitoringOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = monitoring.getIdmonitoring();
                if (findMonitoring(id) == null) {
                    throw new NonexistentEntityException("The monitoring with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Monitoring monitoring;
            try {
                monitoring = em.getReference(Monitoring.class, id);
                monitoring.getIdmonitoring();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The monitoring with id " + id + " no longer exists.", enfe);
            }
            Technicalcollection technicalcollection = monitoring.getTechnicalcollection();
            if (technicalcollection != null) {
                technicalcollection.getMonitoringList().remove(monitoring);
                technicalcollection = em.merge(technicalcollection);
            }
            Surroundingsculture surroundingsculture = monitoring.getSurroundingsculture();
            if (surroundingsculture != null) {
                surroundingsculture.getMonitoringList().remove(monitoring);
                surroundingsculture = em.merge(surroundingsculture);
            }
            Supporttype supporttype = monitoring.getSupporttype();
            if (supporttype != null) {
                supporttype.getMonitoringList().remove(monitoring);
                supporttype = em.merge(supporttype);
            }
            Predominantbiome predominantbiome = monitoring.getPredominantbiome();
            if (predominantbiome != null) {
                predominantbiome.getMonitoringList().remove(monitoring);
                predominantbiome = em.merge(predominantbiome);
            }
            Digitizer digitizer = monitoring.getDigitizer();
            if (digitizer != null) {
                digitizer.getMonitoringList().remove(monitoring);
                digitizer = em.merge(digitizer);
            }
            Denomination denomination = monitoring.getDenomination();
            if (denomination != null) {
                denomination.getMonitoringList().remove(monitoring);
                denomination = em.merge(denomination);
            }
            Culture culture = monitoring.getCulture();
            if (culture != null) {
                culture.getMonitoringList().remove(monitoring);
                culture = em.merge(culture);
            }
            Cultivar cultivar = monitoring.getCultivar();
            if (cultivar != null) {
                cultivar.getMonitoringList().remove(monitoring);
                cultivar = em.merge(cultivar);
            }
            Colorpantrap colorpantrap = monitoring.getColorpantrap();
            if (colorpantrap != null) {
                colorpantrap.getMonitoringList().remove(monitoring);
                colorpantrap = em.merge(colorpantrap);
            }
            Collector collector = monitoring.getCollector();
            if (collector != null) {
                collector.getMonitoringList().remove(monitoring);
                collector = em.merge(collector);
            }
            List<Specimen> specimenList = monitoring.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setMonitoring(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(monitoring);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Monitoring> findMonitoringEntities() {
        return findMonitoringEntities(true, -1, -1);
    }

    public List<Monitoring> findMonitoringEntities(int maxResults, int firstResult) {
        return findMonitoringEntities(false, maxResults, firstResult);
    }

    private List<Monitoring> findMonitoringEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Monitoring.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Monitoring findMonitoring(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Monitoring.class, id);
        } finally {
            em.close();
        }
    }

    public int getMonitoringCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Monitoring> rt = cq.from(Monitoring.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
